package com.leyou.upload.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class LeyouCorsFilter {

    @Bean
    public CorsFilter corsFilter(){
        // 初始化配置对象
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许跨域的域名，*：代表所有域名，如果允许携带cookie，必须指定具体的域名
        configuration.addAllowedOrigin("http://manage.leyou.com");
        // 允许携带cookie
        configuration.setAllowCredentials(true);
        // 允许跨域的方法，*：允许所有方法跨域
        configuration.addAllowedMethod("*");
        // 允许携带的头信息
        configuration.addAllowedHeader("*");

        // 创建配置源对象
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", configuration);
        return new CorsFilter(configSource);
    }
}
